/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author        Notes
 * 2021-10-10     Sherman       first version
 * 2021-11-03     Sherman       Add icu_sample
 */

#include <rtthread.h>
#include "hal_data.h"
#include <rtdevice.h>

#define LED3_PIN    BSP_IO_PORT_01_PIN_06
#define USER_INPUT  "P105"



/* Private functions */
//static fsp_err_t check_for_write_complete(void);
//static fsp_err_t print_to_console(char *p_data);
//static void process_kit_info(void);

/*******************************************************************************************************************//**
 * The RA Configuration tool generates main() and uses it to generate threads if an RTOS is used.  This function is
 * called by main() when no RTOS is used.
 **********************************************************************************************************************/
void hal_entry(void)
{
    while (true)
    {
        rt_thread_mdelay(100);

    }
}

///*******************************************************************************************************************//**
// * This function is called at various points during the startup process.  This implementation uses the event that is
// * called right before main() to set up the pins.
// *
// * @param[in]  event    Where at in the start up process the code is currently at
// **********************************************************************************************************************/
//void R_BSP_WarmStart(bsp_warm_start_event_t event)
//{
//    if (BSP_WARM_START_POST_C == event)
//    {
//        /* C runtime environment and system clocks are setup. */
//        /* Configure pins. */
//        R_IOPORT_Open (&g_ioport_ctrl, &g_bsp_pin_cfg);
//    }
//}
//
///*****************************************************************************************************************
// *  @brief      Prints the message to console
// *  @param[in]  p_msg contains address of buffer to be printed
// *  @retval     FSP_SUCCESS     Upon success
// *  @retval     any other error code apart from FSP_SUCCESS, Write is unsuccessful
// ****************************************************************************************************************/
//static fsp_err_t print_to_console(char *p_data)
//{
//    fsp_err_t err = FSP_SUCCESS;
//    uint32_t len = ((uint32_t)strlen(p_data));
//
//    err = R_USB_Write (&g_basic0_ctrl, (uint8_t*)p_data, len, (uint8_t)g_usb_class_type);
//    /* Handle error */
//    if (FSP_SUCCESS != err)
//    {
//        return err;
//    }
//
//    err = check_for_write_complete();
//    if (FSP_SUCCESS != err)
//    {
//        /* Did not get the event hence returning error */
//        return FSP_ERR_USB_FAILED;
//    }
//    return err;
//}
//
///*****************************************************************************************************************
// *  @brief      Check for write completion
// *  @param[in]  None
// *  @retval     FSP_SUCCESS     Upon success
// *  @retval     any other error code apart from FSP_SUCCESS
// ****************************************************************************************************************/
//static fsp_err_t check_for_write_complete(void)
//{
//    usb_status_t usb_write_event = USB_STATUS_NONE;
//    int32_t timeout_count = UINT16_MAX;
//    fsp_err_t err = FSP_SUCCESS;
//
//    do
//    {
//        err = R_USB_EventGet (&g_basic0_ctrl, &usb_write_event);
//        if (FSP_SUCCESS != err)
//        {
//            return err;
//        }
//
//        --timeout_count;
//
//        if (0 > timeout_count)
//        {
//            timeout_count = 0;
//            err = (fsp_err_t)USB_STATUS_NONE;
//            break;
//        }
//    }while(USB_STATUS_WRITE_COMPLETE != usb_write_event);
//
//    return err;
//}
//
/////*****************************************************************************************************************
//// *  @brief      Process kit information
//// *  @param[in]  None
//// *  @retval     None
//// ****************************************************************************************************************/
////static void process_kit_info(void)
////{
////    uint16_t buffer_index_count = 0x0000;
////    uint16_t adc_data                    = 0;
////    float mcu_temp_f                     = 0;
////    float mcu_temp_c                     = 0;
////    fsp_err_t err = FSP_SUCCESS;
////
////    /* Read die temperature */
////    err = R_ADC_Read (&g_adc_ctrl, ADC_CHANNEL_TEMPERATURE, &adc_data);
////    /* Handle error */
////    if (FSP_SUCCESS != err)
////    {
////        print_to_console ("** R_ADC_Read API failed ** \r\n");
////        /* Turn ON RED LED to indicate fatal error */
////        TURN_RED_ON
////        APP_ERR_TRAP(err);
////    }
////
////    /* Conversion ADC temperature in Fahrenheit */
////    mcu_temp_f = ADCTEMP_AS_F(adc_data);
////    /* Conversion ADC temperature in celsius */
////    mcu_temp_c = ADCTEMP_AS_C(adc_data);
////
////    /* clear kit info buffer before updating data */
////    memset(kitinfo, '\0', 511);
////
////    /* update  predefined text in the buffer */
////    memcpy(kitinfo, (char *)KIT_INFO_PRIMARY_TEXT, strlen((char *)KIT_INFO_PRIMARY_TEXT) );
////
////    /* calculate current data filled length */
////    buffer_index_count = ((uint16_t)(strlen(kitinfo)));
////
////    /* Check for current led frequency */
////    if (BLINK_FREQ_1HZ == g_curr_led_freq)
////    {
////        sprintf((char*)&kitinfo[buffer_index_count],"%s\t%d",p_led_freq,1);
////    }
////    else if (BLINK_FREQ_5HZ == g_curr_led_freq)
////    {
////        sprintf((char*)&kitinfo[buffer_index_count],"%s\t%d",p_led_freq,5);
////    }
////    else if (BLINK_FREQ_10HZ == g_curr_led_freq)
////    {
////        sprintf((char*)&kitinfo[buffer_index_count],"%s\t%d",p_led_freq,10);
////    }
////    else
////    {
////        /* Do Nothing */
////    }
////
////    buffer_index_count = 0U;
////
////    /* kit_processing_data is filled with led frequency details */
////    buffer_index_count =  ((uint16_t)(strlen(kitinfo))) ;
////
////    /* appends the data from current buffer_index_count */
////    sprintf((char *)&kitinfo[buffer_index_count],
////            "%s\t%.02f/%.02f",p_mcu_temp,mcu_temp_f,mcu_temp_c);
////
////    buffer_index_count  = 0U;
////
////    /* update index count */
////    buffer_index_count = ((uint16_t) (strlen(kitinfo)));
////
////    /* update index count */
////    sprintf((char*)&kitinfo[buffer_index_count],"\r\n%s",p_kit_menu_ret);
////
////    /* Print kit menu to console */
////    err = print_to_console(kitinfo);
////    /* Handle error*/
////    if (FSP_SUCCESS != err)
////    {
////        /* Turn ON RED LED to indicate fatal error */
////        TURN_RED_ON
////        APP_ERR_TRAP(err);
////    }
////
////}
////void hal_entry(void)
////{
////    rt_kprintf("\nHello RT-Thread!\n");
////
////    while (1)
////    {
////        rt_pin_write(LED3_PIN, PIN_HIGH);
////        rt_thread_mdelay(200);
////        rt_pin_write(LED3_PIN, PIN_LOW);
////        rt_thread_mdelay(200);
////    }
////}
//
//void irq_callback_test(void *args)
//{
//    rt_kprintf("\n IRQ00 triggered \n");
//}
//
//void icu_sample(void)
//{
//    /* init */
//    rt_uint32_t pin = rt_pin_get(USER_INPUT);
//    rt_kprintf("\n pin number : 0x%04X \n", pin);
//    rt_err_t err = rt_pin_attach_irq(pin, PIN_IRQ_MODE_RISING, irq_callback_test, RT_NULL);
//    if(RT_EOK != err)
//    {
//        rt_kprintf("\n attach irq failed. \n");
//    }
//    err = rt_pin_irq_enable(pin, PIN_IRQ_ENABLE);
//    if(RT_EOK != err)
//    {
//        rt_kprintf("\n enable irq failed. \n");
//    }
//}
//MSH_CMD_EXPORT(icu_sample, icu sample);
